﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class Phone_RobotAnim : MonoBehaviour
{
    Vector3 rot = Vector3.zero;
	float rotSpeed = 10f;//旋转速度

	Animator anim;


	//记录摇杆运动距离
	private float horizontalWrapMode, verticalMove;
	private Vector3 dir;

	private CharacterController cc;
	public float moveSpeed = 5;

	//获取虚拟摇杆
	public Joystick joystick_walk;
	public Joystick joystick_right;

	//实例化摇杆
	public GameObject joy_walk;
	public GameObject joy_right;

	//实例化动画器
	public GameObject robat_anim;

	// Use this for initialization
	void Awake()
	{
		anim = gameObject.GetComponent<Animator>();
		gameObject.transform.eulerAngles = rot;

		cc = GetComponent<CharacterController>();
	}

	/*public override void OnStartLocalPlayer()
	{
		joy_walk.SetActive(true);
		joy_right.SetActive(true);

		robat_anim.SetActive(true);
		//Camera.main.transform.SetParent(transform);
		//Camera.main.transform.localPosition = Vector3.zero;
	}*/

	// Update is called once per frame
	void Update()
	{
		horizontalWrapMode = joystick_walk.Horizontal * moveSpeed;
		verticalMove = joystick_right.Horizontal * moveSpeed;
		CheckKey();
		rot[1] += rotSpeed * Time.fixedDeltaTime * verticalMove;
		gameObject.transform.eulerAngles = rot;

		/*transform.Rotate(0, verticalMove / 10, 0);

		dir = transform.forward * verticalMove;
		cc.Move(dir * Time.deltaTime);*/
	}

	void CheckKey()
	{
		// Walk
		if (horizontalWrapMode > 0)
		{
			anim.SetBool("Walk_Anim", true);
		}
		else if (horizontalWrapMode == 0)
		{
			anim.SetBool("Walk_Anim", false);
		}

		///Rotate Left
		/*if (verticalMove < 0)
		{
			rot[1] -= rotSpeed * Time.fixedDeltaTime;
		}

		// Rotate Right
		if (verticalMove > 0)
		{
			rot[1] += rotSpeed * Time.fixedDeltaTime;
		}
		*/
		// Roll
		if (Input.GetKeyDown(KeyCode.Space))
		{
			if (anim.GetBool("Roll_Anim"))
			{
				anim.SetBool("Roll_Anim", false);
			}
			else
			{
				anim.SetBool("Roll_Anim", true);
			}
		}

		// Close
		if (Input.GetKeyDown(KeyCode.LeftControl))
		{
			if (!anim.GetBool("Open_Anim"))
			{
				anim.SetBool("Open_Anim", true);
			}
			else
			{
				anim.SetBool("Open_Anim", false);
			}
		}
	}
}
